package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.CommentBO;
import com.cskaoyan.bean.topic.TopicCommentPostBO;
import com.cskaoyan.mapper.CommentMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author pengyehua
 * @date 2020/12/9 15:41
 */
@Service
public class WxCommentServiceImpl implements WxCommentService{

    @Autowired
    CommentMapper commentMapper;

    /**
     * 查询评论数量
     * @param valueId
     * @param type
     * @return
     */
    @Override
    public CommentCount queryCommentCount(int valueId, byte type) {
        //添加查询总评论条件
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);

        //添加查询有图评论条件
        CommentExample commentExample1 = new CommentExample();
        CommentExample.Criteria criteria1 = commentExample1.createCriteria();
        criteria1.andValueIdEqualTo(valueId);
        criteria1.andTypeEqualTo(type);
        criteria1.andHasPictureEqualTo(true);

        //查询
        long allCounts = commentMapper.countByExample(commentExample);
        long hasPicCount = commentMapper.countByExample(commentExample1);

        CommentCount commentCount = new CommentCount();
        commentCount.setAllCount((int) allCounts);
        commentCount.setHasPicCount((int)hasPicCount);
        return commentCount;
    }

    /**
     * 查询所有评论
     * @param map
     * @return
     */
    @Override
    public CommentData queryComment(CommentBO commentBO) {
        //查询评论数
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(commentBO.getValueId());
        criteria.andTypeEqualTo(commentBO.getType());
        long l = commentMapper.countByExample(commentExample);

        //分页
        PageHelper.startPage(commentBO.getPage(),commentBO.getSize());
        //查询评论内容
        List<CommentVO> data = commentMapper.selectComments(commentBO);
        CommentData commentData = new CommentData();
        commentData.setData(data);
        commentData.setCount((int)l);
        commentData.setCurrentPage(commentBO.getPage());
        return commentData;
    }

    @Override
    @Transactional
    public Comment insertComment(TopicCommentPostBO bo,Integer userId) {
        Comment comment = new Comment();
        comment.setType((byte) bo.getType());
        comment.setValueId(bo.getValueId());
        comment.setContent(bo.getContent());
        comment.setStar((short) bo.getStar());
        comment.setHasPicture(bo.isHasPicture());
        comment.setPicUrls(bo.getPicUrls().toArray(new String[0]));
        comment.setAddTime(new Date());
        comment.setUpdateTime(new Date());
        comment.setUserId(userId);
        commentMapper.insert(comment);
        int i = commentMapper.selectMaxId();
        return commentMapper.selectByPrimaryKey(i);
    }
}
